package com.mychat.controller;


import com.mychat.annotation.GlobalInterceptor;
import com.mychat.entity.Constants;
import com.mychat.entity.dto.TokenUserInfoDto;
import com.mychat.entity.po.UserContact;
import com.mychat.entity.query.UserContactQuery;
import com.mychat.entity.vo.GroupInfoVO;
import com.mychat.entity.vo.ResponseVO;
import com.mychat.entity.po.GroupInfo;
import com.mychat.entity.query.GroupInfoQuery;
import com.mychat.enums.GroupInfoStatusEnum;
import com.mychat.enums.MessageTypeEnum;
import com.mychat.enums.ResponseCodeEnum;
import com.mychat.enums.UserContactStatusEnum;
import com.mychat.exception.BusinessException;
import com.mychat.redis.RedisComponent;
import com.mychat.redis.RedisUtils;
import com.mychat.service.GroupInfoService;
import com.mychat.service.UserContactService;
import org.apache.el.parser.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

import java.io.IOException;
import java.util.List;
/**
 * @Description:Controller
 * @auther:YYT
 * @date:2025/03/02
 */
@RestController
@RequestMapping("/group")
public class GroupInfoController extends ABaseController {

	@Resource
	private GroupInfoService groupInfoService;
    @Resource
	private UserContactService userContactService;

	@RequestMapping("/saveGroup")
	@GlobalInterceptor
	public ResponseVO saveGroup(HttpServletRequest request, String groupId, @NotEmpty String groupName, String groupNotice, @NotNull Integer joinType, MultipartFile avatarFile, MultipartFile avatarCover) throws BusinessException, IOException {
		TokenUserInfoDto tokenUserInfoDto = getTokenUserInfo(request);
		GroupInfo groupInfo = new GroupInfo();
		groupInfo.setGroupId(groupId);
		groupInfo.setGroupName(groupName);
		groupInfo.setGroupOwnerId(tokenUserInfoDto.getUserId());
		groupInfo.setGroupNotice(groupNotice);
		groupInfo.setJoinType(joinType);
		this.groupInfoService.saveGroup(groupInfo, avatarFile, avatarCover);
		return getSuccessResponseVO(null);
	}

	@RequestMapping("/loadMyGroup")
	@GlobalInterceptor
	public ResponseVO loadMyGroup(HttpServletRequest request) {
		TokenUserInfoDto tokenUserInfo = getTokenUserInfo(request);
		GroupInfoQuery query = new GroupInfoQuery();
		query.setGroupOwnerId(tokenUserInfo.getUserId());
		query.setOrderBy("create_time desc");
		List<GroupInfo> groupInfoList = this.groupInfoService.findListByParam(query);
		return getSuccessResponseVO(groupInfoList);
	}

	@RequestMapping("/loadGroupInfo")
	@GlobalInterceptor
	public ResponseVO loadGroupInfo(HttpServletRequest request, @NotEmpty String groupId) throws BusinessException {
		GroupInfo groupInfo = getGroupDetailCommon(request, groupId);
		UserContactQuery query = new UserContactQuery();
		query.setContactId(groupId);
		Integer memberCount = this.userContactService.findCountByParam(query);
		groupInfo.setMemberCount(memberCount);
		return getSuccessResponseVO(groupInfo);
	}

	private GroupInfo getGroupDetailCommon(HttpServletRequest request, @NotEmpty String groupId) throws BusinessException {
		TokenUserInfoDto tokenUserInfo = getTokenUserInfo(request);
		UserContact userContact = this.userContactService.getUserContactByUserIdAndContactId(tokenUserInfo.getUserId(), groupId);
		if(null == userContact || !UserContactStatusEnum.FRIEND.getCode().equals(userContact.getStatus())) {
			throw new BusinessException("你不在群聊或群聊不存在或已经解散");
		}
		GroupInfo groupInfo = this.groupInfoService.getGroupInfoByGroupId(groupId);
		if(null == groupInfo || !GroupInfoStatusEnum.NORMAL.getCode().equals(groupInfo.getStatus())) {
			throw new BusinessException("群聊不存在或已经解散");
		}
		return groupInfo;
	}

	@RequestMapping("/getGroupInfo4Chat")
	@GlobalInterceptor
	public ResponseVO getGroupInfo4Chat(HttpServletRequest request, @NotEmpty String groupId) throws BusinessException {
		GroupInfo groupInfo = getGroupDetailCommon(request, groupId);
		UserContactQuery query = new UserContactQuery();
		query.setContactId(groupId);
		query.setQueryUserInfo(true);
		query.setOrderBy("create_time asc");
		query.setStatus(UserContactStatusEnum.FRIEND.getCode());
		List<UserContact> userContactList = this.userContactService.findListByParam(query);
		GroupInfoVO groupInfoVO = new GroupInfoVO();
		groupInfoVO.setGroupInfo(groupInfo);
		groupInfoVO.setUserContactList(userContactList);
		return getSuccessResponseVO(groupInfoVO);
	}

	@RequestMapping("/addOrRemoveGroupUser")
	@GlobalInterceptor
	public ResponseVO addOrRemoveGroupUser(HttpServletRequest request, @NotEmpty String groupId, @NotEmpty String selectContacts, @NotNull Integer opType) throws BusinessException {
		TokenUserInfoDto tokenUserInfo = getTokenUserInfo(request);
		groupInfoService.addOrRemoveGroupUser(tokenUserInfo, groupId, selectContacts, opType);
		return getSuccessResponseVO(null);
	}

	@RequestMapping("/leaveGroup")
	@GlobalInterceptor
	public ResponseVO leaveGroup(HttpServletRequest request, @NotEmpty String groupId) throws BusinessException {
		TokenUserInfoDto tokenUserInfo = getTokenUserInfo(request);
		groupInfoService.leaveGroup(tokenUserInfo.getUserId(), groupId, MessageTypeEnum.LEAVE_GROUP);
		return getSuccessResponseVO(null);
	}

	@RequestMapping("/dissolutionGroup")
	@GlobalInterceptor
	public ResponseVO dissolutionGroup(HttpServletRequest request, @NotEmpty String groupId) throws BusinessException {
		TokenUserInfoDto tokenUserInfo = getTokenUserInfo(request);
		groupInfoService.dissolutionGroup(tokenUserInfo.getUserId(), groupId);
		return getSuccessResponseVO(null);
	}
}